package com.ruoyi.dida.db.table.controller;

import java.util.List;

import com.ruoyi.common.utils.Constants;
import com.ruoyi.common.utils.ReturnT;
import com.ruoyi.dida.db.manager.service.DBService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.dida.db.table.domain.CatalogueTable;
import com.ruoyi.dida.db.table.service.ICatalogueTableService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 表列表Controller
 * 
 * @author ljh
 * @date 2020-08-12
 */
@Controller
@RequestMapping("/table/table")
public class CatalogueTableController extends BaseController
{
    private String prefix = "table/table";

    @Resource
    private DBService dbService;

    @Autowired
    private ICatalogueTableService catalogueTableService;

    @RequiresPermissions("table:table:view")
    @GetMapping()
    public String table()
    {
        return prefix + "/table";
    }

    /**
     * 查询表列表列表
     */
    @RequiresPermissions("table:table:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CatalogueTable catalogueTable) {
        startPage();
        List<CatalogueTable> list = catalogueTableService.selectCatalogueTableList(catalogueTable);
        return getDataTable(list);
    }

    /**
     * 导出表列表列表
     */
    @RequiresPermissions("table:table:export")
    @Log(title = "表列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CatalogueTable catalogueTable) {
        List<CatalogueTable> list = catalogueTableService.selectCatalogueTableList(catalogueTable);
        ExcelUtil<CatalogueTable> util = new ExcelUtil<CatalogueTable>(CatalogueTable.class);
        return util.exportExcel(list, "table");
    }

    /**
     * 新增表列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存表
     */
    @RequiresPermissions("table:table:add")
    @Log(title = "表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CatalogueTable catalogueTable) {
        ReturnT returnT = dbService.addTable(catalogueTable.getDbNameSource(), catalogueTable.getTableNameSource(), Constants.DBNAME, catalogueTable.getCatalogueTableName());
        System.out.println("创建表=="+returnT);

        return toAjax(catalogueTableService.insertCatalogueTable(catalogueTable));
    }

    /**
     * 修改表列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CatalogueTable catalogueTable = catalogueTableService.selectCatalogueTableById(id);
        mmap.put("catalogueTable", catalogueTable);
        return prefix + "/edit";
    }

    /**
     * 修改保存表列表
     */
    @RequiresPermissions("table:table:edit")
    @Log(title = "表列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CatalogueTable catalogueTable) {
        return toAjax(catalogueTableService.updateCatalogueTable(catalogueTable));
    }

    /**
     * 删除表列表
     */
    @RequiresPermissions("table:table:remove")
    @Log(title = "表列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(catalogueTableService.deleteCatalogueTableByIds(ids));
    }
}
